home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-21 | 10.3 KB | 396 lines | [TEXT/CWIE] |
- #include "ocheaders.h"
- #include "BDDISPIDs.h"
- #include "BDConsts.h"
- #include "CBaseControl.h"
- #include "CBaseEventClient.h"
- #include "BDUtils.h"
- #include "BDAssert.h"
- #include "FnAssert.h"
- #include "dispatch.h"
- #include <LArray.h>
- #include <LArrayIterator.h>
- #include "CConnectionPoint.h"
- #include "CCPContainer.h"
- #include <stdio.h>
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // Constants
- //
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // class statics
- //
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::CBaseEventClient
- //
- CBaseEventClient::CBaseEventClient(void)
- {
- m_Cookie = 0; // NOTE: if we ever actually use this for something, we need a
- // list of them, since we allow connections to more than
- // one specific sources.
-
- mIncomingInterfaceID = kUninitializedInterfaceID;
-
- SetSourceName("");
-
- for ( long i = 0; i < MAX_NUM_SOURCE_CONTROLS; i++ )
- mDesiredSources[i] = NULL;
- mNumDesiredSources = -1;
-
- for ( long i = 0; i < MAX_NUM_SOURCE_CONTROLS; i++ )
- mFoundSources[i] = NULL;
- mNumFoundSources = 0;
-
- mConnectingComplete = false;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::IUnknown::QueryInterface
- //
-
- STDMETHODIMP
- CBaseEventClient::QueryInterface(REFIID inRefID, void** outObj)
- {
- ErrorCode result = CBaseCOM::QueryInterface(inRefID, outObj);
-
- if ( result == E_NOINTERFACE )
- {
- void* pv = nil;
-
- if (inRefID == IID_IPersist || inRefID == IID_IPersistPropertyBag)
- pv = (void*)(IPersistPropertyBag*) this;
-
- *outObj = pv;
-
- // if we got an interface, ref it and return ok
- if ( pv )
- {
- ((IUnknown*) pv)->AddRef();
- result = S_OK;
- }
- }
-
- return result;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::~CBaseEventClient
- //
-
- CBaseEventClient::~CBaseEventClient(void)
- {
- if ( mSourceName )
- {
- delete [] mSourceName;
- mSourceName = NULL;
- }
-
- if ( mNumDesiredSources > 0 )
- {
- for ( long i = 0; i < mNumDesiredSources; i++ )
- {
- assert(mDesiredSources[i] != NULL);
- delete [] mDesiredSources[i];
- mDesiredSources[i] = NULL;
- }
- mNumDesiredSources = -1;
- }
-
- if ( mNumFoundSources > 0 )
- {
- for ( long i = 0; i < mNumFoundSources; i++ )
- {
- assert(mFoundSources[i] != NULL);
- delete [] mFoundSources[i];
- mFoundSources[i] = NULL;
- }
- mNumFoundSources = 0;
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::IPersistPropertyBag::Load
- //
-
- STDMETHODIMP
- CBaseEventClient::Load(IPropertyBag * propertyBag,IErrorLog * errorLog)
- {
- char propertyString[Str255BufferLength];
- long i = 0;
-
- // try to load in each property. if we can't get it, then leave
- // things at the default.
-
- char propertyNameString[64];
-
- // loop through all the sourceobject[x] parameters. It's possible there's only one, with no [x]
- strcpy(propertyNameString, SOURCEOBJECT_STR);
- if ( ::LoadPropertyString(propertyBag, propertyNameString, propertyString, Str255StringLength, errorLog) )
- AddDesiredSourceName(propertyString);
- else
- {
- for ( i = 0; i <= MAX_NUM_SOURCE_CONTROLS; i++ )
- {
- sprintf(propertyNameString, "%s[%ld]", SOURCEOBJECT_STR, i);
- if ( ::LoadPropertyString(propertyBag, propertyNameString, propertyString, Str255StringLength, errorLog) )
- AddDesiredSourceName(propertyString);
- }
- }
-
- return ResultFromScode(S_OK);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::AddOutIncomingInterface
- //
- Boolean
- CBaseEventClient::AddIncomingInterface(IID incomingInterfaceID)
- {
- Boolean addedIt = false;
-
- // right now, we only support one incoming interface.
- assert(mIncomingInterfaceID == kUninitializedInterfaceID);
- mIncomingInterfaceID = incomingInterfaceID;
-
- addedIt = true;
-
- return addedIt;
- }
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::SetUpConnections
- //
- void
- CBaseEventClient::SetUpConnections(IContainerSite * containerSite, IContainer * container)
- {
- // if the client site has been set and we haven't yet tried to connect,
- // enumerate the other controls in this container to find the one we want
- // to connect to. We use the mConnectingComplete, mNumDesiredSources, and
- // mNumFoundSources to handle the case where some of the controls we're
- // attempting to connect to aren't enumerated yet because they haven't
- // loaded yet. Once we find all the ones we're looking for, we stop
- // looking.
- //
- // Note that we used to check to advisory cookie (m_Cookie) instead of
- // mConnectingComplete, but in our case now, we want to be able to connect
- // to multiple specific sources, so we need a separate flag.
-
- if (containerSite != NULL && !mConnectingComplete)
- {
- // if we don't have a container yet, get it now
- if ( !container )
- {
- containerSite->GetContainer(&container);
- if ( container )
- container->AddRef();
- }
-
- if ( ! container )
- return;
-
- IUnknown* testControl;
- IEnumUnknown* enumU;
-
- // Get the ICon interface, then the container
- //m_pClientSite->QueryInterface(IID_IClientSite, (void**) &clientSite);
- //clientSite->GetContainer(&container);
-
- // Enumerate the objects
- if ( SUCCEEDED(container->EnumControls(nil, OLECONTF_EMBEDDINGS, &enumU)) )
- {
- IConnectionPointContainer* cpContainer = nil;
-
- while ( enumU->Next(1, &testControl, nil) == NOERROR )
- {
- // get the name. Do it here instead of in a more nested scope so that
- // we can use if for debugging.
- char controlName[Str255BufferLength];
- GetObjectName (testControl, controlName);
-
- char controlID[Str255BufferLength];
- GetObjectID (testControl, controlID);
-
- // Try to get a connection point container from the control
- testControl->QueryInterface(IID_IConnectionPointContainer, (void**) &cpContainer);
-
- // if it has one, this may be it
- if ( cpContainer )
- {
- IConnectionPoint* cp = nil;
-
- // See if this connection point container
- // has a connection point with the outgoing interface we want
- cpContainer->FindConnectionPoint(mIncomingInterfaceID, &cp);
-
- // if we got it, set an advise connection on the outgoing interface
- if ( cp )
- {
- Boolean setAdviseSink = false;
-
- if ( mNumDesiredSources < 0 ) // we're not being specific
- {
- AddSource(controlID); // we don't really care about this if
- // mNumDesiredSources < 0, but we'll do it
- // anyway for the sake completeness.
- setAdviseSink = true;
- }
- else
- {
- for ( long i = 0; i < mNumDesiredSources; i++ )
- {
- ASSERT(mDesiredSources[i] != NULL, "Unexpected NULL source name!");
- if ( ! strcmp(mDesiredSources[i], controlID) ) // it's one of our targets
- {
- if ( AddSource(controlID) ) // if it wasn't already added
- setAdviseSink = true;
- break; // no need to loop any further once we've matched
- }
- }
- }
-
- // We need the cast to CBaseCOM here to avoid ambiguous access compiler errors
- if ( setAdviseSink )
- cp->Advise((CBaseCOM *)this, &m_Cookie);
- }
-
- cpContainer->Release();
- }
- }
-
- // Set a reminder to indicate that we've already done our connecting.
- if ( mNumDesiredSources < 0 )
- mConnectingComplete = true;
- else
- mConnectingComplete = (mNumDesiredSources == mNumFoundSources);
-
- // Don't forget to release
- enumU->Release();
- }
- else
- m_Cookie = -1;
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::IsSource
- //
- Boolean CBaseEventClient::IsSource(IUnknown * unk)
- {
- Boolean isSource = false;
-
- if ( mNumDesiredSources == -1 ) // we're not being specific
- isSource = true;
- else
- {
- char sourceName[256];
- GetObjectName (unk, sourceName);
-
- if ( sourceName )
- isSource = IsSource(sourceName);
- }
-
- return isSource;
- }
-
- Boolean CBaseEventClient::IsSource(char * sourceName)
- {
- Boolean isSource = false;
-
- if ( mNumDesiredSources == -1 ) // we're not being specific
- isSource = true;
- else
- {
- for ( long i = 0; i < mNumFoundSources; i++ )
- {
- ASSERT(mFoundSources[i] != NULL, "Unexpected NULL source name!");
- if ( ! strcmp(mFoundSources[i], sourceName) ) // it's one of our targets
- {
- isSource = true;
- break; // no need to loop any further once we've matched
- }
- }
- }
-
- return isSource;
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::SetSourceName
- //
- void CBaseEventClient::SetSourceName(char * theName)
- {
- if ( ! mSourceName )
- mSourceName = (char *) new char[Str255BufferLength];
-
- strcpy(mSourceName, theName);
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::AddDesiredSourceName
- //
- void CBaseEventClient::AddDesiredSourceName(char * theName)
- {
- ASSERT(theName != NULL, "Unexpected NULL source name in AddDesiredSourceName");
-
- long currentIndex = (mNumDesiredSources < 0 ? 0 : mNumDesiredSources);
- long nextIndex = currentIndex + 1;
-
- if ( nextIndex < MAX_NUM_SOURCE_CONTROLS )
- {
- mDesiredSources[currentIndex] = new char[strlen(theName)+1];
- if ( mDesiredSources[currentIndex] )
- {
- strcpy(mDesiredSources[currentIndex], theName);
- mNumDesiredSources = nextIndex;
- }
- }
- }
-
- ///////////////////////////////////////////////////////////////////////////////
- //
- // CBaseEventClient::AddSource
- //
- Boolean
- CBaseEventClient::AddSource(char * sourceName)
- {
- // only add the source to our list of found sources if we haven't already
- // done so.
-
- Boolean addedIt = false;
-
- Boolean alreadyExists = false;
-
- for ( long i = 0; i < mNumFoundSources; i++ )
- {
- if ( ! strcmp(mFoundSources[i], sourceName) ) // it's already in our list
- {
- alreadyExists = true;
- break;
- }
- }
-
- if ( ! alreadyExists )
- {
- mFoundSources[mNumFoundSources] = new char[strlen(sourceName)+1];
- if ( mFoundSources[mNumFoundSources] )
- {
- strcpy(mFoundSources[mNumFoundSources], sourceName);
- mNumFoundSources++;
- addedIt = true;
- }
- }
-
- return addedIt;
- }
-